********************************************************************************
**	PURPOSE: Identify heterogeneity in credit behaviors, create T6, AT6
**							
**	INPUTS: Long with outcomes
**	
**	OUTPUTS: Credit Behaviors_post_avg_effects_flag_randomized - Table 6
**			 Credit Behaviors_post_het_effects_flag_randomized - Table 6
**			 Credit Behaviors_endlines_avg effects - Appendix Table 6
**			 Credit Behaviors_post_avg effects - Appendix Table 6
**			 
**  CREATED/EDITED BY: Kayla Wilding, Leah Kim, Hasan Ahamed
**
**	DATE CREATED: 7/2018
**
**	DATE LAST EDITED: 3/1/2023
********************************************************************************

/*******************************************************************************
0. Setup 
********************************************************************************/
clear all

*Filename and table title
local filename1  "Credit Behaviors_post_het groups"
local filename1a "Credit Behaviors_post_avg effects"
local filename2  "Credit Behaviors_endlines_het groups"
local filename2a "Credit Behaviors_endlines_avg effects"

*Define the covariate regression spec options and the outcome variables of interest
local outcomes  z_newcredit_i z_default_index_all_i z_amountsowed_i z_utilization_i $creditmix //outcomes 
local outcomes2  z_newcredit_i z_default_index_all_i //outcomes for 1b and 2b
local decs 3		//decimals 
local lowter ltca	//lower tercile of installment credit activity at baseline index
local midter mtca	//middle tercile of installment credit activity at baseline index 
local highter htca	//upper tercile of installment credit activity at baseline index

*Spec for average treatment effects 
*By post 
local loanspec1a post 1.enc#1.post	
*By endline
local loanspec2a pt1 pt2 pt3 1.enc#1.pt1 1.enc#1.pt2 1.enc#1.pt3

*Spec for heterogeneous treatment effect by post 
*By post 
local loanspec1h 1.`lowter'#1.post 1.`midter'#1.post 1.`highter'#1.post 1.`lowter'#1.enc#1.post 1.`midter'#1.enc#1.post 1.`highter'#1.enc#1.post 
*By endline 
local loanspec2h pt1 pt2 pt3 1.`lowter'#1.pt1 1.`lowter'#1.pt2 1.`lowter'#1.pt3 1.`lowter'#1.enc#1.pt1 1.`lowter'#1.enc#1.pt2 1.`lowter'#1.enc#1.pt3 1.`highter'#1.enc#1.pt1 1.`highter'#1.enc#1.pt2 1.`highter'#1.enc#1.pt3

/*******************************************************************************
1. Column 1a, 2a, 3, 4, 5 
********************************************************************************/
*Import data
use "$adta/Long with outcomes.dta", clear

*Regression loop with output 
 foreach j in 1 {
	local wordcount = 1
	foreach regvar in `outcomes' {
		foreach restrict in "flag_randomized" "flag_randomized_scored_base"  {
			cap drop __*
			local wordtot : word count `outcomes'
			areg `regvar' `loanspec`j'a' if `restrict' == 1, absorb(surveyid) vce(cluster surveyid)
			local inds = e(df_a) + 1
			local obs = e(N)
			
		*adding control means for different period in the table
			sum `regvar' if e(sample) == 1 & post == 1 & flag_extragroup == 1
			if r(mean) >= -0.001 & r(mean) <= 0.00 {
				local mu_lhs_post = string(abs(r(mean)), "%14.`decs'f") 
			}  //end if 
			else {
				local mu_lhs_post = string(r(mean), "%14.`decs'f") 
			}           
			sum `regvar' if e(sample) == 1 & index == 6 & flag_extragroup == 1
			if r(mean) >= -0.001 & r(mean) <= 0.00 {
				local mu_lhs_6 = string(abs(r(mean)), "%14.`decs'f") 
			 }  //end if 
			else {
				local mu_lhs_6 = string(r(mean), "%14.`decs'f") 
			} 
			sum `regvar' if e(sample) == 1 & index == 12 & flag_extragroup == 1
			if r(mean) >= -0.001 & r(mean) <= 0.00 {
				local mu_lhs_12 = string(abs(r(mean)), "%14.`decs'f") 
			}  //end if 
			else {
				local mu_lhs_12 = string(r(mean), "%14.`decs'f") 
			} 
			sum `regvar' if e(sample) == 1 & index == 18 & flag_extragroup == 1
			if r(mean) >= -0.001 & r(mean) <= 0.00 {
				local mu_lhs_18 = string(abs(r(mean)), "%14.`decs'f") 
			}  //end if 
			else {
				local mu_lhs_18 = string(r(mean), "%14.`decs'f") 
			}        
			sum `regvar' if e(sample) == 1 & index == 1 & flag_extragroup == 1
			if r(mean) >= -0.001 & r(mean) <= 0.00 {
				local mu_lhs = string(abs(r(mean)), "%14.`decs'f") 
			} 	//end if 
			else {
				local mu_lhs = string(r(mean), "%14.`decs'f") 
			}                     
			if "`regvar'" == "`: word 1 of `outcomes''" {
				local action = "replace"
			}   //end regvar 
			else {
				local action = "append"
			}       
			#delimit ;                              
			outreg2 using "$outputtables/`filename`j'a'_`restrict'.xls", `action' excel label
				nocons noobs dec(`decs') nor2 addtext(Obs, `obs', Individuals, `inds', 
				Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs', 
				Mean Dep Var In Extra Step at Post, `mu_lhs_post', 
				Mean Dep Var In Extra Step at 6, `mu_lhs_6', 
				Mean Dep Var In Extra Step at 12, `mu_lhs_12', 
				Mean Dep Var In Extra Step at 18, `mu_lhs_18') keep(1.enc#1.post)
				slow(8000) nonote noaster;
			#delimit cr;
			sleep 2000 
			cap drop __*
	
			areg `regvar' `loanspec`j'h' if `restrict' == 1 /* & `=cond("`wordcount'" == "`wordtot'", "op18", "`restrict'")' == 1 */ , absorb(surveyid) vce(cluster surveyid)
			local obs = e(N)
			local inds = e(df_a) + 1
			sum `regvar' if e(sample) == 1 & index == 1 & flag_extragroup == 1
			if r(mean) >= -0.001 & r(mean) <= 0.00 {
				local mu_lhs = string(abs(r(mean)), "%14.`decs'f") 
			} 	 //end if 
			else {
				local mu_lhs = string(r(mean), "%14.`decs'f") 
			}  // end else
			if `j' == 1 {
				di "j `j'"
				test 1.`lowter'#1.enc#1.post = 1.`midter'#1.enc#1.post
				local pvalue1_2 = string(r(p), "%14.`decs'f")
				test 1.`midter'#1.enc#1.post = 1.`highter'#1.enc#1.post
				local pvalue2_3 = string(r(p), "%14.`decs'f")
				test 1.`lowter'#1.enc#1.post = 1.`highter'#1.enc#1.post
				local pvalue1_3 = string(r(p), "%14.`decs'f")
				
				if "`regvar'" == "`: word 1 of `outcomes''" {
					local action = "replace"
				} 	 //end regvar 
				else {
					local action = "append"
				}  	//end else  
				
				#delimit ;                              
				outreg2 using "$outputtables/`filename`j''_`restrict'.xls", `action' excel label 
					nocons noobs dec(`decs') nor2 
					addtext(P-value of 1.`lowter'#1.enc#1.post = 1.`midter'#1.enc#1.post, `pvalue1_2',
							P-value of 1.`midter'#1.enc#1.post = 1.`highter'#1.enc#1.post, `pvalue2_3', 
							P-value of 1.`lowter'#1.enc#1.post = 1.`highter'#1.enc#1.post, `pvalue1_3',
							Obs, `obs', Individuals, `inds', Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs')  
					slow(8000) nonote noaster;
				#delimit cr;
				sleep 2000 
				//output a blank column
				outreg2 using "$outputtables/`filename`j''_`restrict'.xls", append skip
			}  //end j == 1
			else if `j' == 2 {
				di "j `j'"
				foreach var in "1.`lowter'#1.enc#1.pt1" "1.`lowter'#1.enc#1.pt2" "1.`lowter'#1.enc#1.pt3" "1.`highter'#1.enc#1.pt1" "1.`highter'#1.enc#1.pt2" "1.`highter'#1.enc#1.pt3" {
					foreach group in "1.`lowter'#1.enc#1.pt1" "1.`lowter'#1.enc#1.pt2" "1.`lowter'#1.enc#1.pt3" "1.`highter'#1.enc#1.pt1" "1.`highter'#1.enc#1.pt2" "1.`highter'#1.enc#1.pt3" {
						test "`var'" = "`group'"
						local name = subinstr(subinstr("p`var'`group'", "#","",.), ".","",.)
						local `name' = string(r(p), "%14.`decs'f") 
						di "`name'"
					}  //end foreach group
				}	 //end foreach var
				if "`restrict'" == "flag_randomized" & "`regvar'" == "`: word 1 of `outcomes''"  {
					local action "replace"
				} 
				else {
					local action "append"
				} 
				#delimit ;
				outreg2 using "$outputtables/`filename`j''_`restrict'.xls", `action' 
					keep(1.`lowter'#1.enc#1.pt1 1.`lowter'#1.enc#1.pt2 1.`lowter'#1.enc#1.pt3 
					1.`highter'#1.enc#1.pt1 1.`highter'#1.enc#1.pt2 1.`highter'#1.enc#1.pt3) 
					label nocons noobs dec(`decs') nor2 addtext(
					P-Value for ltca_enc_pt1 = ltca_enc_pt2, `p1ltca1enc1pt11ltca1enc1pt2', 
					P-Value for ltca_enc_pt1 = ltca_enc_pt3, `p1ltca1enc1pt11ltca1enc1pt3',         
					P-Value for ltca_enc_pt2 = ltca_enc_pt3, `p1ltca1enc1pt21ltca1enc1pt3', 
					P-Value for htca_enc_pt1 = htca_enc_pt2, `p1htca1enc1pt11htca1enc1pt2', 
					P-Value for htca_enc_pt1 = htca_enc_pt3, `p1htca1enc1pt11htca1enc1pt3',         
					P-Value for htca_enc_pt2 = htca_enc_pt3, `p1htca1enc1pt21htca1enc1pt3',         
					P-Value for ltca_enc_pt1 = htca_enc_pt1, `p1ltca1enc1pt11htca1enc1pt1',         
					P-Value for ltca_enc_pt2 = htca_enc_pt2, `p1ltca1enc1pt21htca1enc1pt2',
					P-Value for ltca_enc_pt3 = htca_enc_pt3, `p1ltca1enc1pt31htca1enc1pt3', Obs, `obs', 
					Individuals, `inds', Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs') slow(8000) nonote noaster;
				#delimit cr;
				if "`restrict'" == "flag_randomized_scored_base" {      
					outreg2 using "$outputtables/`filename`j''_`restrict'.xls", append skip
				}  //end of add blank column
			} 	 //end j == 2
		}	//end restrict
		local ++wordcount
	} //ends regvar

}

/*******************************************************************************
2. Table 6 Column 1b, 2b
********************************************************************************/
*Import data
use "$adta/Long with outcomes.dta", clear
drop if index == 6 // Drop 6-month endline, change from do file 07. 

*Regression loop with output 
loc j 1
local wordcount = 1
foreach regvar in `outcomes2' {
	foreach restrict in "flag_randomized"  {
		cap drop __*
		local wordtot : word count `outcomes2'
		areg `regvar' `loanspec`j'a' if `restrict' == 1 /* & `=cond("`wordcount'" == "`wordtot'", "op18", "`restrict'")' == 1 */, absorb(surveyid) vce(cluster surveyid)
		local inds = e(df_a) + 1
		local obs = e(N)
		sum `regvar' if e(sample) == 1 & post == 1 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs_post = string(abs(r(mean)), "%14.`decs'f") 
		}  //end if 
		else {
			local mu_lhs_post = string(r(mean), "%14.`decs'f") 
		}           
		sum `regvar' if e(sample) == 1 & index == 12 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs_12 = string(abs(r(mean)), "%14.`decs'f") 
		}  //end if 
		else {
			local mu_lhs_12 = string(r(mean), "%14.`decs'f") 
		} 
		sum `regvar' if e(sample) == 1 & index == 18 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs_18 = string(abs(r(mean)), "%14.`decs'f") 
		}  //end if 
		else {
			local mu_lhs_18 = string(r(mean), "%14.`decs'f") 
		}        
		sum `regvar' if e(sample) == 1 & index == 1 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs = string(abs(r(mean)), "%14.`decs'f") 
		} 	//end if 
		else {
			local mu_lhs = string(r(mean), "%14.`decs'f") 
		}                     
		if "`regvar'" == "`: word 1 of `outcomes''" {
			local action = "replace"
		}   //end regvar ==
		else {
			local action = "append"
		}       
		#delimit ;                              
		outreg2 using "$outputtables/`filename`j'a'_`restrict'_2.xls", `action' excel label
			nocons noobs dec(`decs') nor2 addtext(Obs, `obs', Individuals, `inds', 
			Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs', 
			Mean Dep Var In Extra Step at Post, `mu_lhs_post', 
			Mean Dep Var In Extra Step at 12, `mu_lhs_12', 
			Mean Dep Var In Extra Step at 18, `mu_lhs_18')  
			slow(8000) nonote noaster;
		#delimit cr;
		sleep 2000 
		cap drop __*
				areg `regvar' `loanspec`j'h' if `restrict' == 1 /* & `=cond("`wordcount'" == "`wordtot'", "op18", "`restrict'")' == 1 */ , absorb(surveyid) vce(cluster surveyid)
		local obs = e(N)
		local inds = e(df_a) + 1
		sum `regvar' if e(sample) == 1 & index == 1 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs = string(abs(r(mean)), "%14.`decs'f") 
		} 	 //end if 
		else {
			local mu_lhs = string(r(mean), "%14.`decs'f") 
		}  // end else
		if `j' == 1 {
				di "j `j'"
				test 1.`lowter'#1.enc#1.post = 1.`midter'#1.enc#1.post
				local pvalue1_2 = string(r(p), "%14.`decs'f")
				test 1.`midter'#1.enc#1.post = 1.`highter'#1.enc#1.post
				local pvalue2_3 = string(r(p), "%14.`decs'f")
				test 1.`lowter'#1.enc#1.post = 1.`highter'#1.enc#1.post
				local pvalue1_3 = string(r(p), "%14.`decs'f")
				
				if "`regvar'" == "`: word 1 of `outcomes''" {
					local action = "replace"
				} 	 //end regvar == 
				else {
					local action = "append"
				}  	//end else  
				
				#delimit ;                              
				outreg2 using "$outputtables/`filename`j''_`restrict'_2.xls", `action' excel label 
					nocons noobs dec(`decs') nor2 
					addtext(P-value of 1.`lowter'#1.enc#1.post = 1.`midter'#1.enc#1.post, `pvalue1_2',
							P-value of 1.`midter'#1.enc#1.post = 1.`highter'#1.enc#1.post, `pvalue2_3', 
							P-value of 1.`lowter'#1.enc#1.post = 1.`highter'#1.enc#1.post, `pvalue1_3',
							Obs, `obs', Individuals, `inds', Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs')  
					slow(8000) nonote noaster;
				#delimit cr;
				sleep 2000 
				//output a blank column
				outreg2 using "$outputtables/`filename`j''_`restrict'_2.xls", append skip
			}  //end j == 1
	}	//end restrict
		local ++wordcount
} //ends regvar


/*******************************************************************************
3. Column 6 of Table 6 
********************************************************************************/
* Import data
use "$adta/Long with outcomes.dta", clear

local decs 3

*Baseline means 
sum cm_delinquent if enc==0 & flag_randomized==1 & index==1
local bd_flag_randomized = string(r(mean), "%14.`decs'f") 
sum cm_delinquent if enc==0 & flag_randomized_scored_base==1 & index==1
local bd_flag_randomized_scored_base = string(r(mean), "%14.`decs'f") 
*Post means 
sum cm_delinquent if enc==0 & flag_randomized==1 & post==1
local pd_flag_randomized = string(r(mean), "%14.`decs'f") 
sum cm_delinquent if enc==0 & flag_randomized_scored_base==1  & post==1
local pd_flag_randomized_scored_base = string(r(mean), "%14.`decs'f") 
	
*Panel A ----------------------------------------------------------------------- 
*Regressions 
drop if index==1
foreach restrict in "flag_randomized" "flag_randomized_scored_base" {
	di in red "`j' `regvar' `restrict'"
	di in red "`pd_`restrict''"
	di in red "`bd_`restrict''"
	cap drop __*
	reg cm_delinquent enc if `restrict'==1, vce(cluster surveyid)
	local inds = e(df_r) + 1
	local obs = e(N)
	#de ; 
	outreg2 using "$outputtables/Credit Behaviors_post_avg effects_`restrict'.xls", append
		keep(enc) label nocons noobs dec(`decs') nor2 addtext(Obs, `obs', Individuals, `inds',
		Mean Dep Var In Extra Step at Post, `pd_`restrict'') slow(8000) nonote noaster ; 
	#de cr 
}	
				
*Panel B : Het groups ---------------------------------------------------------

local lowter ltca	//lower tercile of installment credit activity at baseline index
local midter mtca	//middle tercile of installment credit activity at baseline index 
local highter htca	//upper tercile of installment credit activity at baseline index

*Regressions 
foreach restrict in "flag_randomized" "flag_randomized_scored_base" {
	di in red "`j' `regvar' `restrict'"
	cap drop __*
	reg cm_delinquent 1.`lowter'#1.enc 1.`midter'#1.enc 1.`highter'#1.enc if `restrict'==1, vce(cluster surveyid)
	local inds = e(df_r) + 1
	local obs = e(N)
	test 1.`lowter'#1.enc = 1.`midter'#1.enc
	local pvalue1_2 = string(r(p), "%14.`decs'f")
	test 1.`midter'#1.enc = 1.`highter'#1.enc
	local pvalue2_3 = string(r(p), "%14.`decs'f")
	test 1.`lowter'#1.enc = 1.`highter'#1.enc
	local pvalue1_3 = string(r(p), "%14.`decs'f")
	#d ; 
	outreg2 using "$outputtables/Credit Behaviors_post_het groups_`restrict'.xls", append 
		keep(1.`lowter'#1.enc 1.`midter'#1.enc 1.`highter'#1.enc) label nocons noobs dec(`decs') nor2 
		addtext(Obs, `obs', Individuals, `inds', 
		Mean Dependent Variable in Extra Step Group at Baseline, "NA",
		P-value of 1.`lowter'#1.enc = 1.`midter'#1.enc, `pvalue1_2', 
		P-value of 1.`midter'#1.enc = 1.`highter'#1.enc, `pvalue2_3', 
		P-value of 1.`lowter'#1.enc = 1.`highter'#1.enc, `pvalue1_3') slow(8000) nonote noaster ; 
	#d cr 
}

	
/*******************************************************************************
4. Column 7 of Table 6 
********************************************************************************/

*Use this specification for column 7 of table six, but first collapse so that there is only one observation per person
*first have to replace cases where people didn't take up CBL with zeros
*Import data
use "$adta/Long with outcomes.dta", clear
local decs 3

replace cm30plus = 0 if cm30plus==. & op18==0 
tab cm30plus op18, missing

mdesc cm_delinquent if index == 1 // no missings
mdesc cm_delinquent if index == 6 // 6 missing 
mdesc cm_delinquent if index == 12 // 12 missing
mdesc cm_delinquent if index == 18 // 19 missing

***issues related to number of observations***
*browse surveyid index op18 cm_open cm_delinquent cm30plus if missing(cm_delinquent) 
/* Explanation: For some survey ids, CBL delinquency == 1 is 0 in baseline but missing for other endlines (all CBL takers). When we create variable indicating ever delinquent, we take the maximum of delinquency for each observation. So for the observations that have only 0s and missings in CBL delinquency == 1, ever delinquent come out as 0. Therefore, number of individuals are not the same for CBL delinquency == 1 and ever delinquent. 
Solution: Change ever delinquent to missing for relevant survey ids.  
*/

gen miss_delinquent = missing(cm_delinquent)
egen nummiss_delinquent = total(miss_delinquent), by(surveyid)
replace cm30plus = . if nummiss_delinquent == 3

***check number of observations***
distinct surveyid if flag_randomized == 1 & inlist(index,6,12,18) // 1531
assert r(ndistinct) == 1531
distinct surveyid if ~missing(cm_delinquent) & flag_randomized == 1 & inlist(index,6,12,18) // 1525
assert r(ndistinct) == 1525
distinct surveyid if ~missing(cm30plus) & flag_randomized == 1 & inlist(index,6,12,18) // 1525
assert r(ndistinct) == 1525
distinct surveyid if ~missing(cm30plus) & ~missing(htca) & ~missing(mtca) & ~missing(ltca) & flag_randomized == 1 & inlist(index,6,12,18)
assert r(ndistinct) == 1496

*Collapse 
collapse (max) cm30plus enc flag_randomized flag_randomized_scored_base pt1 pt2 pt3 op18 ltca mtca htca, by(surveyid)
distinct surveyid if ~missing(cm30plus) & flag_randomized == 1

reg cm30plus enc if flag_randomized == 1, vce(cluster surveyid)		

*Get post means 
sum cm30plus if enc==0 & flag_randomized==1
local b3_flag_randomized = string(r(mean), "%14.`decs'f") 
sum cm30plus if enc==0 & flag_randomized_scored_base==1
local b3_flag_randomized_scored_base = string(r(mean), "%14.`decs'f") 

*Panel A ----------------------------------------------------------------------- 

*Regressions 
foreach restrict in "flag_randomized" "flag_randomized_scored_base" {
	cap drop __*
	reg cm30plus enc if `restrict'==1, vce(cluster surveyid)
	local inds = e(df_r) + 1
	local obs = e(N)
	#de ; 
	outreg2 using "$outputtables/Credit Behaviors_post_avg effects_`restrict'.xls", append
		keep(enc) label nocons noobs dec(`decs') nor2 addtext(Obs, `obs', Individuals, `inds',
		Mean Dep Var In Extra Step at Post, `b3_`restrict'') slow(8000) nonote noaster ; 
	#de cr 
}	
				
*Panel B : Het groups  --------------------------------------------------------- 
local lowter ltca	//lower tercile of installment credit activity at baseline index
local midter mtca	//middle tercile of installment credit activity at baseline index 
local highter htca	//upper tercile of installment credit activity at baseline index

*Regressions 
foreach restrict in "flag_randomized" "flag_randomized_scored_base" {
	cap drop __*
	reg cm30plus 1.`lowter'#1.enc 1.`midter'#1.enc 1.`highter'#1.enc if `restrict'==1, vce(cluster surveyid)
	local inds = e(df_r) + 1
	local obs = e(N)
	test 1.`lowter'#1.enc = 1.`midter'#1.enc
	local pvalue1_2 = string(r(p), "%14.`decs'f")
	test 1.`midter'#1.enc = 1.`highter'#1.enc
	local pvalue2_3 = string(r(p), "%14.`decs'f")
	test 1.`lowter'#1.enc = 1.`highter'#1.enc
	local pvalue1_3 = string(r(p), "%14.`decs'f")
	#d ; 
	outreg2 using "$outputtables/Credit Behaviors_post_het groups_`restrict'.xls", append 
		keep(1.`lowter'#1.enc 1.`midter'#1.enc 1.`highter'#1.enc) label nocons noobs dec(`decs') nor2 
		addtext(Obs, `obs', Individuals, `inds', 
		Mean Dependent Variable in Extra Step Group at Baseline, "NA", 
		P-value of 1.`lowter'#1.enc = 1.`midter'#1.enc, `pvalue1_2', 
		P-value of 1.`midter'#1.enc = 1.`highter'#1.enc, `pvalue2_3', 
		P-value of 1.`lowter'#1.enc = 1.`highter'#1.enc, `pvalue1_3') slow(8000) nonote noaster ; 
	#d cr 
}	

**EOF**
